<?php
namespace app\home\controller;
use app\home\controller;
use app\home\model\UserM;
use myResponse\Res;
use think\Db;
use think\Request;

//用户控制器
class User extends Base{
    //显示用户列表
    function userList($page = 1,$size = 10,$searchText = ''){
        $page *=1;
        $size *=1;
//        $where = "UserState = 'Y' ";
        $where = "";
        if($searchText){
//            $where .= " AND ( LoginName LIKE '%{$searchText}%' OR UserName LIKE '%{$searchText}%' )";
            $where .= " LoginName LIKE '%{$searchText}%' OR UserName LIKE '%{$searchText}%' ";
        }


        $total = Db::name('sys_user')->where($where)->count();
/*        $list = Db::view('sys_user', '*')
            ->view('sys_role', '*', 'sys_user.RoleId=sys_role.RoleId')
            ->view('sys_department', 'DeptId,DeptNO,DeptName', 'sys_user.DeptId=sys_department.DeptId')
            ->where('UserState','=','Y')->paginate($size,$total);*/
        $list = Db::view('sys_user', '*')
            ->view('sys_role', '*', 'sys_user.RoleId=sys_role.RoleId', 'left')
            ->view('sys_department', 'DeptId,DeptNO,DeptName', 'sys_user.DeptId=sys_department.DeptId', 'left')
            ->where($where)
            ->order('sys_user.CreateTime desc')
            ->page($page,$size)
            ->select();
//        df($list);
//        df($page);
//        $totalPage = ceil($total / $page);
        Res::suc('ok',[
            'total'=>$total,
            'page'=>$page,
            'size'=>$size,
            'list'=>$list
        ]);
    }

    //添加用户
    function addUser(){
        //在UserModel里边通过一个指定方法实现权限添加
        if(input('LoginName') == ""){
            Res::errs('请输入用户名称！');
        }
        if(input('UserName') == ""){
            Res::errs('请输入真实名称！');
        }
        if(input('UserPwd') == ""){
            Res::errs('请输入密码名称！');
        }
        if(input('RoleId') == ""){
            Res::errs('请选择角色！');
        }
        if(input('DeptId') == ""){
            Res::errs('请选择部门！');
        }
        $user= new UserM;
        $z=$user->addUser();
        if($z){
            Res::suc('添加用户成功！');
        }else {
            Res::errs('添加用户失败！');
        }
    }
    //修改用户
    function editUser(){
        //在UserModel里边通过一个指定方法实现权限添加
        if(input('LoginName') == ""){
            Res::errs('请输入用户名称！');
        }
        if(input('UserName') == ""){
            Res::errs('请输入真实名称！');
        }
        if(input('RoleId') == ""){
            Res::errs('请选择角色！');
        }
        if(input('DeptId') == ""){
            Res::errs('请选择部门！');
        }
        $user= new UserM;
        $z=$user->editUser();
        if($z){
            Res::suc('修改用户成功！');
        }else {
            Res::errs('修改用户失败！');
        }

    }

}
